package aleksandar.djuric.entities;

import java.util.Collections;
import java.util.LinkedList;

import org.apache.commons.lang3.StringEscapeUtils;


public class Team {
	private String name;
	private int id;
	private LinkedList<Player> players;
	public static String nL = System.getProperty("line.separator");
	
	public Team(String name, int id)
	{
		this.name = StringEscapeUtils.unescapeHtml4(name);
		this.id = id;
	}
	
	private LinkedList<Player> getPlayers()
	{
		if(players == null)
			return players = new LinkedList<Player>();
		else 
		{
			Collections.sort(players);
			return players;
		}
	}
	
	public void addPlayer(Player p)
	{
		getPlayers().add(p);
	}
	
	public void addPlayers(LinkedList<Player> players)
	{
		this.players = players;
	}
	
	public Player getPlayer(int i)
	{
		if(i >= 0 && i < getPlayers().size())
			return players.get(i);
		else
			return null;
	}
	
	public String[] getPlayerNames()
	{
		String[] s = new String[getPlayers().size()];
		for (int i = 0; i < s.length; i++) {
			s[i] = players.get(i).getName();
		}
		return s;
	}
	
	public Player getPlayer(String name)
	{
		for (int i = 0; i < getPlayers().size(); i++) {
			if(players.get(i).getName().equals(name))
				return players.get(i);
		}
		return null;
	}
	
	public int getNPlayers()
	{
		return getPlayers().size();
	}
	
	public String getName() 
	{
		return name;
	}



	public int getId() {
		return id;
	}

	public String toStringPlayers()
	{
		StringBuffer sb = new StringBuffer();
		sb.append(name);
		sb.append(nL);
		sb.append(id);
		sb.append(nL + nL);
		for (int i = 0; i < players.size(); i++) {
			sb.append(players.get(i) + nL);
		}
		return sb.toString();
	}
	
	public String toStringDisplay()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<b>");
		sb.append(name);
		sb.append("<br>");
		sb.append(id);
		sb.append("<br><br>");
		sb.append("<table " +
		"border=\"0\"" +
//		"cellspacing=\"0\"" +
//		"cellpadding=\"0\"" +
		"align=\"center\"" +
		"width=\"95%\"" +
		"style=\"" +
		"font-weight: bold;" +
//		"font-family:sans-serif;" +
//		"font-size: 12" +
		"\"" +
		">");
		if(players.size() > 0)
			sb.append("<tr><th>No</th><th>User</th><th>Name</th></tr>");
		Player p = null;
		for (int i = 0; i < players.size(); i++) {
			p = players.get(i);
			sb.append("<tr><th width=5%>" +
					p.getNumber() +
					"</th><th>" +
					p.getUserName() +
					"</th><th>" +
					"<a href=\"" +
					i +
					"\" style=\"color:"
			);
			if(p.getActivity() != Player.ACTIVE)
				sb.append((
						Integer.toHexString(
								p.getActivityColor().getRGB())).substring(2));
			sb.append("\" >" + 
					p.getName() +
					"</a>" +				
					"</th></tr>"
			);
		}
		sb.append("</table>");
		return sb.toString();
	}


	@Override
	public String toString() {
		return name;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Team other = (Team) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public static void main(String[] args) {

	}

}


